<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    
	
	<title>_调拨申请</title>
	<script type="text/javascript">
	
	    String.prototype.replaceAll = function(s1,s2) { 
		    return this.replace(new RegExp(s1,"gm"),s2); 
		};
		
        $(function() {
		    $("#queryForm").validate(); 
		    $("#inputForm").validate(); 
			$("#_fromDate").datepicker({ format : 'yyyy-mm-dd' });
			$("#_toDate").datepicker({ format : 'yyyy-mm-dd' });
       });
       
       
       function allCheck() {
		 $("input[name='var_check']").attr("checked", $("#var_allCheck").is(":checked"));
		
		 if($("#var_allCheck").is(":checked")){
		    $('input[name="var_check"]:checked').each(function(){
		        var info=$(this).attr("id");
		        if(infos.indexOf(info+";")==-1){
		          infos+=info+";";
		         }
		         
			});
		 }else{
		   infos='';
		 }
	  }
		   
	  function check(element) {
	    var info=$(element).attr("id");
	     
		if ($(element).is(":checked")) {       //选中
		    if(infos.indexOf(info+";")==-1){
		       infos+=info+';';
		    }
		} else {                  //取消选中
			infos=infos.replaceAll( (info+';') , '' );
			$("#var_allCheck").attr("checked", false);
		}
	  }
       
       //弹出商品选择框
       function showGoodsWindow(){
	      var outstorageCode=$("#_outstorageCode").val();
	      var outstorageName=$("#_outstorageCode").find("option:selected").text();
	      
	      $("#outstorageCode").val(outstorageCode);
	      $("#outstorageName").val(outstorageName);
	      
		  $("#goodsGrid").html("");
		  $("#var_allCheck").attr("checked", false);
		}
		
		var infos="";
		
		//查询商品
		function queryGoods(){
	       var outstorageCode=$("#outstorageCode").val();
	       var sku=$("#sku").val();
	       
	       if(outstorageCode==''){
	          alert("请返回选择调出仓库...");
	          return false;
	       }
	       
	       $.ajax({
				type : 'post',
				url : '${ctx}/allot/ajaxQueryGoodsStock',
				data : {
					outstorageCode:outstorageCode,
					sku:sku
				},
				dataType : 'json',
				success : function(data) {
					var goodsStockList = data.goodsStockList;
					$("#goodsGrid").html("");
					
					var str='';
					$.each(goodsStockList, function(i) {
					    
					    var  goodsStock=goodsStockList[i];
					    var info=goodsStock.sku+"_"+goodsStock.areagroup.groupCode+"_"+goodsStock.customer.customerCode+"_"+goodsStock.salesNum;
					    var  exist_flag=false;
					    
					    if(infos.indexOf(info)!=-1){
					       exist_flag=true;
					    }
					    
					    str +="<tr>";
					    if(exist_flag==true){
					      str += "<td style='text-align: center;'><input type='checkbox' id='"+info+"' name='var_check'  onclick='check(this);' checked=true></td>";
					    }else{
					      str += "<td style='text-align: center;'><input type='checkbox' id='"+info+"' name='var_check'  onclick='check(this);' ></td>";
					    }
						
						str += "<td style='width:200px;'>" + goodsStock.customer.customerName+ "</td>";
						str += "<td style='width:200px;'>" + goodsStock.sku + "</td>";
						str += "<td>" + goodsStock.salesNum + "</td>";
						str +="</tr>";
						
					});
					
					$("#goodsGrid").html(str);
				},
				error:function() {
					alert("网络繁忙，请稍后重试...");
				}
			}); 
	      
		}
		
	  //选择商品
	  function select(){
	    var infoArray=infos.split(";");
	    for(var i=0;i<infoArray.length-1;i++){
	    
	       info=infoArray[i];
	       var tempArray=info.split("_");
	       var sku=tempArray[0];
	       var outstorageCode=tempArray[1];
	       var customerCode=tempArray[2];
	       var salesNum=tempArray[3];
	       var temp=sku+"_"+outstorageCode+"_"+customerCode;
	       var flag=0;

           $('tr[name=tr_allot]').each(function(){
	             var id=$(this).attr("id");
	             if(id.indexOf(temp)!=-1){
	                flag=1;
	             }
           });
           
           if(flag==0){
               var tr="";
		       tr+="<tr id='tr_"+info+"' name='tr_allot'>";
		       tr+="<td ><span>"+outstorageCode+"</span><span id='message1_"+info+"' style='color:red;'></span></td>";
		       tr+="<td>"+customerCode+"</td>";
		       tr+="<td>"+sku+"</td>";
		       tr+="<td id='salesNum_"+info+"'>"+salesNum+"</td>";
		       tr+="<td><input id='allotNum_"+info+"' type='text' /><span  id='message_"+info+"' style='color:red;'></span></td>";
		       tr+="<td><input type='button' class='btn btn-danger' onClick='del(this);' id='btn_"+info+"' value='移除' /></td>";
		       tr+="</tr>";
		       $("#goodsGrid_1").append(tr);
           }
	       
	    }
	     
	  }
	  
	  //移除
	  function del(element){
	     //删除列
	     var id=$(element).attr("id").substring(4);
	     var tr_id="#tr_"+id;
	     $(tr_id).remove();
	     //减去信息
	     infos=infos.replaceAll( (id+';') , '' );
	  }
	  
	  //提交申请
	  function mysubmit(){
	      var flag=0;
	      var infoArray=new Array();
	      var _outstorageCode=$("#_outstorageCode").val();
	      
	      var _instorageCode=$("#_instorageCode").val();
	      if(_instorageCode==''){
	        alert("请选择调入仓库...");
	        $("#_instorageCode").focus();
	        return ;
	      }
	      
	      var record=$('tr[name=tr_allot]');
	      
	      if(record.length==0){
	       alert("请选择需要调拨的商品...");
	       return ;
	      }
	      
          var itemInfo="";  
	      record.each(function(){
	             var id=$(this).attr("id").substring(3);
	             var flag_1=0;
	             
	             var tempArray=id.split("_");
	             var sku=tempArray[0];
	             var customerCode=tempArray[2];
	             var outstorageCode=tempArray[1];
	             
	             var salesNum_id="#salesNum_"+id;
	             var allotNum_id="#allotNum_"+id;
	             var message_id="#message_"+id;
	             var message1_id="#message1_"+id;
	             var tr_id="#tr_"+id;
	             
	             var salesNum=$(salesNum_id).html();
	             var allotNum=$(allotNum_id).val();
	             
	             itemInfo+=customerCode+"_"+sku+"_"+allotNum+";";
	             
	             if(_outstorageCode!=outstorageCode){
	                $(message1_id).html(" 调出仓库不一致！");
	                flag=1;
	                flag_1=1;
	             }else{
	                $(message1_id).html("");
	             }
	             
	             
	             if(isNaN(allotNum) || allotNum==''){
	                $(message_id).html(" 请输入整数！");
	                flag=1;
	                flag_1=1;
	             }
	             else if(Number(salesNum)<Number(allotNum)){
	                $(message_id).html(" 调拨数量不能多于现库存！");
	                flag=1;
	                flag_1=1;
	             }
	             else{
	                $(message_id).html("");
	             }
	             
	             
	             if(flag_1==1){
	               $(tr_id).css("background-color",'yellow');
	             }else{
	               $(tr_id).css("background-color",'');
	             }
                 	             
           });
           
           if(flag==1){  //验证不通过
              alert("调拨申请数据异常,请检查核对...");
              return;
           }else{   //验证通过，提交信息
           
		         $("#allotItemText").val(itemInfo);
		         $("#outstorageCode").val(_outstorageCode);
		         $("#instorageCode").val(_instorageCode);
              
                 $("#inputForm").submit();
              
           }
	  
	  }
		
       
    </script>
</head>

<body>

    <c:if test="${not empty message}">
		<div id="message" class="alert alert-success"><button data-dismiss="alert" class="close">×</button>${message}</div>
	</c:if>
	
	<div style="height: 10px;"></div>
	
	<legend><small>调拨申请</small></legend>
	
	
	<div class="row">
		<div class="span12" style="margin-left: 20px;">
			<form class="form-search">
			
				<label style="margin-left:10px;">调出仓库：</label> 
				<select  name="_outstorageCode"  id="_outstorageCode" value="${param._outstorageCode}"  class="input-large">
					<option value=""></option>
					<c:forEach items="${areagroupList}" var="areagroup">
						<option value="${areagroup.groupCode}"
						<c:if test="${param._outstorageCode eq areagroup.groupCode}">selected=selected</c:if>
						>${areagroup.groupName}</option>
					</c:forEach>
				</select>
				
				<label style="margin-left:10px;">调入仓库：</label> 
				<select  name="_instorageCode"  id="_instorageCode" value="${param._instorageCode}"  class="input-large">
					<option value=""></option>
					<c:forEach items="${areagroupList}" var="areagroup">
						<option value="${areagroup.groupCode}"
						<c:if test="${param._instorageCode eq areagroup.groupCode}">selected=selected</c:if>
						>${areagroup.groupName}</option>
					</c:forEach>
				</select>
				
				<a  href="#selectGoods" role="button" data-toggle="modal" class="btn btn-info" onclick="showGoodsWindow();" style="margin-left: 10px;" >选择商品</a>
		    </form>
	    </div>
	</div>
	
	<form id="inputForm" action="${ctx}/allot/create" method="post" class="form-horizontal">
	<fieldset>
	    <input  type="hidden" name="allotItemText" id="allotItemText" value="${allotItemText}"/>
	    <input  type="hidden" name="outstorageCode" id="outstorageCode" value="${outstorageCode}"/>
	    <input  type="hidden" name="instorageCode" id="instorageCode" value="${instorageCode}"/>
	    <input  type="hidden" name="id" id="id" value="${id}"/>
	    
		<table id="contentTable" class="table  table-bordered table-condensed">
			<thead><tr>
			    <th style="width: 80px;">调出仓库</th>
			    <th style="width: 80px;">商家</th>
			    <th style="width: 80px;">sku编码</th>
			    <th style="width: 80px;">现库存量</th>
				<th style="width: 80px;">调拨数量</th>
				<th style="width: 150px;">管理</th>
			</tr></thead>
			<tbody id="goodsGrid_1"></tbody>
		</table>
		<div class="form-actions">
			<input id="submit_btn" class="btn btn-primary" type="button" style="" value="提交申请" onclick="mysubmit();"/>&nbsp;	
			<input id="cancel_btn" class="btn" type="button" value="返回" onclick="history.back()"/>
	   </div>
	   
	 </fieldset>  
	</form>
	
	
	<!-- 选择商品窗体 ---------------------------------------------------------------------------------------->
	<div style="display:none; width:700px;" class="modal hide fade" role="dialog" id="selectGoods" >
			<!-- 头 -->
			<div class="modal-header" style="height: 40px;margin-top: 5px;" >
			  <form class="form-search">
			        <label  class="control-label">调出仓库:</label>
					<input type="text"    id="outstorageName" name="outstorageName"  class="input-medium" disabled="disabled" />
					<input type="hidden"  id="outstorageCode" name="outstorageCode"  class="input-medium"/>
		   
			        <label class="control-label" style="margin-left: 10px;">sku编码:</label>
					<input type="text"  id="sku" name="sku"  class="input-large" />
		     
			        <button type="button" style="margin-left: 10px;"  class="btn" onclick="queryGoods();">搜索</button>
			  </form>
			</div>
		
			<!-- 体 -->
			<div class="modal-body" style="height: 300px;">
		      <table id="contentTable" class="table table-striped table-bordered table-condensed">
				<thead><tr>
				    <th style="width:30px; text-align: center;"><input type="checkbox" name="var_allCheck" id="var_allCheck" onclick="allCheck()" /></th>
					<th>商家</th>
					<th>sku</th>
					<th>现库存量</th>
				</tr></thead>
				<tbody id="goodsGrid"></tbody>
			</table>
		 </div>
			
		<!-- 尾 -->
		<div class="modal-footer">
			<button   onclick="select();" class="btn btn-primary"  data-dismiss="modal"  > 确定 </button>
			<a class="btn" data-dismiss="modal" aria-hidden="true">取消</a>
		</div>
		
    </div>
    <!-- 选择商品窗体 ---------------------------------------------------------------------------------------->
	
</body>
</html>
